export const request = (config) => {
    const bashUrl = 'http://127.0.0.1:8899/'
    // 处理 apiUrl
    if (!config.address) {
		config.url = bashUrl + config.url;
    }


    if (!config.data) {
        config.data = {};
    }

    let promise = new Promise(function(resolve, reject) {
        config.header = {
            'Authorization': uni.getStorageSync('token'),
            'Content-Type': 'application/json'
        }
        uni.request(config).then(responses => {
            console.log(responses)
            if (responses[0]) {
                uni.showToast({
                    title: "网络异常",
                    duration: 2000,
                    icon: "none"
                })
                return
            }
            const requestMethod = (responses[1]?.config?.method || '').toLowerCase();
            if (responses[1].data.code === 401 || responses[1].data.code === 402 && requestMethod === "post") {
                let token = ""
                uni.setStorageSync('token', token)
                uni.navigateTo({
                    url: '/pages/login/login'
                })
            }
            resolve(responses[1].data);
        });
    }).catch(error => {
        reject(error)
    })

    return promise;
};


export default request;
